package jp.kentaro.plugin.sample.jfreechart.meterchart1;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DialShape;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.data.Range;
import org.jfree.data.Value;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class ViewPart extends org.eclipse.ui.part.ViewPart {
	private Double val = 0.0;
	private Shell shell;
	private Display display;
	private ChartComposite frame;
	private JFreeChart chart;
	private Composite composite;
	private MeterPlot plot;
	
	public ViewPart() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		shell = parent.getShell();
		display = shell.getDisplay();
		composite = parent;
		chart = createChart();
		frame = new ChartComposite(composite, SWT.NONE, chart, true);
		
		Runnable runnable = new Runnable(){
			public void run() {
				Display display = shell.getDisplay();
				//frame.update();
				val += 0.1;
				chart = createChart();
				frame.setChart(chart);
				frame.forceRedraw();
				if (!display.isDisposed()) {
					display.timerExec(500, this);
				}
			}
		};
		display = shell.getDisplay();
		if(!display.isDisposed())
			display.timerExec(500, runnable);
		
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	/**
    * Creates the Chart based on a dataset
    */
   private JFreeChart createChart() {
      DefaultValueDataset data = new DefaultValueDataset(val);
      plot = new MeterPlot(data);
      plot.setUnits("Degrees");
      plot.setRange(new Range(0.0, 140.0));
      
      JFreeChart chart = new JFreeChart("Meter Chart",
      JFreeChart.DEFAULT_TITLE_FONT, plot, false);
      plot.setNoDataMessage("No data available");
      plot.setDialShape(DialShape.CHORD);
      return chart;
   }

	public void paintControl(PaintEvent e) {
		// TODO Auto-generated method stub
		
	}


}
